/*
  使用示例：

  弹窗要加载的组件
  import popdemo from './popdemo.vue'

  打开弹窗
  dialogParams：传给dialog组件的参数
  childParams：传给子组件的参数

  async showDialogExp() {
    const res = await this.$showDialog({
      component: popdemo,
      dialogParams: {
        title: '编辑数据源',
        width: '320px',
      },
      childParams: { t: 123 },
    })
    console.log('#############', res)
  }

  更新日志：
  v1.0.1 - 2024-02-11
  1、初步实现函数式弹窗封装
*/
const VER = '1.0.1'

import X2DialogComp from './X2Dialog.vue'
import Vue from 'vue'

let instance
let initInstance = (Dialog) => {
  let DialogConstructor = Vue.extend(Dialog)
  instance = new DialogConstructor({
    el: document.createElement('div'),
  })
}

const X2Dialog = (options) => {
  const _dialog = Object.assign({}, X2DialogComp)
  initInstance(_dialog)
  document.body.appendChild(instance.$el)

  Vue.nextTick(function () {
    let arrProp = Object.getOwnPropertyNames(options)
    arrProp.forEach(function (prop) {
      instance[prop] = options[prop]
    })
    instance['ver'] = VER
    instance._showDialog()
  })

  if (typeof Promise !== 'undefined') {
    return new Promise((resolve, reject) => {
      instance.resolve = resolve
      instance.reject = reject
    })
  }
  return instance
}

X2Dialog._showDialog = (p) => {
  return X2Dialog(p)
}

export default X2Dialog
